#!/usr/bin/env python3
# -*- Coding:UTF-8 -*-
#  9:30
from pprint import pprint

import pymysql


class MysqlTool(object):
    def __init__(self,conn_info):
        self.conn_info=conn_info

    def __enter__(self):
        self.conn=pymysql.connect(**self.conn_info)
        self.curs=self.conn.cursor()
        return self

    #增删改的方法（事务：insert update delete）,pymysql自动开启事务
    def cud_data(self,sql):
        for i in sql:
            self.curs.execute(i)

    #查询
    def query_data(self,sql):
        self.curs.execute(sql)
        return self.curs.fetchall()

    def __exit__(self, exc_type, exc_val, exc_tb):
        self.curs.close()
        self.conn.close()


if __name__ == '__main__':
    db_connect=dict(
        host='localhost',
        user='root',
        password='',
        port=3306,
        database='woniusales',
        charset='utf8'
    )
    with MysqlTool(db_connect) as mt:
        data=mt.query_data('select * from user')
    pprint(data)

